package com.llkj.qingnianguoxue.book;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseFragment;

/**
 * 订阅
 */
public class BookFragment extends BaseFragment implements View.OnClickListener{
    private FrameLayout flVideo, flBooks,flMasters;//视频，图书，大师
    private BookVideoFragment videoFragment;//视频
    private BookBooksFragment booksFragment;//图书
    private BookMastersFragment mastersFragment;//大师

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_zhibo, container, false);
        setTitle("订阅", false, -1, -1, true,TITLE_TYPE_IMG, R.drawable.icon_circle_search);
        rlTitle.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorFE4A49));
        tvTitle.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
        titleLineBottom.setVisibility(View.GONE);

        initView();
        initListener();

        setSelection(0);
        return rootView;
    }

    private void initView(){
        flVideo = (FrameLayout) rootView.findViewById(R.id.fl_book_video);
        flBooks = (FrameLayout) rootView.findViewById(R.id.ll_book_books);
        flMasters = (FrameLayout) rootView.findViewById(R.id.ll_book_master);
    }
    private void initListener(){
        flVideo.setOnClickListener(this);
        flBooks.setOnClickListener(this);
        flMasters.setOnClickListener(this);
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        String category = null;
        if(flVideo.isSelected()){
            category = "1";
        }else if(flBooks.isSelected()){
            category = "2";
        }else if(flMasters.isSelected()){
            category = "3";
        }
        Bundle bundle = new Bundle();
        bundle.putString("searchCategory",category);
        startActivity(SearchSubscribeActivity.class,bundle);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fl_book_video://视频
                setSelection(0);
                break;
            case R.id.ll_book_books://我图书
                setSelection(1);
                break;
            case R.id.ll_book_master://大师
                setSelection(2);
                break;
        }
    }

    private void setSelection(int index) {
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index) {
            case 0://
                setSelected(flVideo);
                if (videoFragment == null) {
                    videoFragment = new BookVideoFragment();
                    Bundle bundle = new Bundle();
                    videoFragment.setArguments(bundle);

                    fragmentTransaction.add(R.id.content_layout, videoFragment, "tab1");
                } else {
                    fragmentTransaction.show(videoFragment);
                }
                fragmentTransaction.commit();
                break;
            case 1://
                setSelected(flBooks);
                if (booksFragment == null) {
                    booksFragment = new BookBooksFragment();
                    Bundle bundle = new Bundle();
                    booksFragment.setArguments(bundle);

                    fragmentTransaction.add(R.id.content_layout, booksFragment, "tab2");
                } else {
                    fragmentTransaction.show(booksFragment);
                }

                fragmentTransaction.commit();
                break;
            case 2://
                setSelected(flMasters);
                if (mastersFragment == null) {
                    mastersFragment = new BookMastersFragment();
                    Bundle bundle = new Bundle();
                    mastersFragment.setArguments(bundle);

                    fragmentTransaction.add(R.id.content_layout, mastersFragment, "tab3");
                } else {
                    fragmentTransaction.show(mastersFragment);
                }

                fragmentTransaction.commit();
                break;
        }
    }

    private void setSelected(View view) {
        flVideo.setSelected(false);
        flBooks.setSelected(false);
        flMasters.setSelected(false);
        view.setSelected(true);
    }

    /**
     * 隐藏所有的fragment
     *
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        if (videoFragment != null) {
            transaction.hide(videoFragment);
        }
        if (booksFragment != null) {
            transaction.hide(booksFragment);
        }
        if (mastersFragment != null) {
            transaction.hide(mastersFragment);
        }
    }
}
